package cn.doctor.assistant.pad.ui.ziliao;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.doctor.assistant.pad.R;
import cn.doctor.assistant.pad.entity.Record;

/**
 * Created by jiaolf on 2016/4/5.
 */
public class RecordListAdapter extends BaseAdapter{

    private int mLayoutResourceId;
    private Context context;
    private List<Record> items;

    public RecordListAdapter(Context context,
                             int layoutResourceId,
                             List<Record> data) {

        this.context = context;
        this.mLayoutResourceId = layoutResourceId;
        this.items = data;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        Record record = items.get(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(context)
                    .inflate(mLayoutResourceId, parent, false);
            holder = new ViewHolder();

            holder.id = (TextView) convertView.findViewById(R.id.list_item_id);
            holder.name = (TextView) convertView.findViewById(R.id.list_item_name);
            holder.keshi = (TextView) convertView.findViewById(R.id.list_item_keshi);
            holder.doctor = (TextView) convertView.findViewById(R.id.list_item_main_doctor);
            holder.diagnose = (TextView) convertView.findViewById(R.id.list_item_diagnose);
            holder.zhuyuanhao = (TextView) convertView.findViewById(R.id.list_item_zhuyuan_number);
            holder.startDate = (TextView) convertView.findViewById(R.id.list_item_start_date);
            holder.endDate = (TextView) convertView.findViewById(R.id.list_item_end_date);
            holder.bingCheng = (TextView) convertView.findViewById(R.id.list_item_bingcheng);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.id.setText(String.valueOf(position + 1));
        holder.name.setText(record.getName());
        holder.keshi.setText(record.getDept());
        holder.doctor.setText(record.getMajor());
        holder.zhuyuanhao.setText(record.getInhiscode());
        holder.startDate.setText(TextUtils.isEmpty(record.getAdmissionDate()) ? "" : record.getAdmissionDate().substring(0,10));
        holder.endDate.setText(TextUtils.isEmpty(record.getDischargeDate()) ? "" : record.getDischargeDate().substring(0,10));
        holder.bingCheng.setText("");

        return convertView;
    }

    class ViewHolder {
        TextView id;
        TextView name;
        TextView keshi;
        TextView doctor;
        TextView diagnose;
        TextView zhuyuanhao;
        TextView startDate;
        TextView endDate;
        TextView bingCheng;
    }
}
